{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "efd2c150-9b60-44e7-be0e-180d92197270",
   "metadata": {},
   "source": [
    "# LLamaIndex工具调用\n",
    "\n",
    "吴恩达《使用LlamaIndex构建主动式RAG|Building Agentic RAG with LlamaIndex》\n",
    "\n",
    "https://www.bilibili.com/video/BV18m421u7zD?p=3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "09584257-f86b-4097-b4df-f296b4cb3e70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数字孪生（Digital Twin）是一种先进的建模技术，它通过创建物理实体（如机器、设备、系统或过程）的虚拟副本，来模拟和分析其在现实世界中的行为和性能。这个虚拟副本通常是基于物理实体的实时数据和历史数据构建的，可以用来预测、优化和监控实体的运行状态。\n",
      "\n",
      "数字孪生的核心概念包括：\n",
      "\n",
      "1. **模型构建**：使用传感器收集的实时数据、历史数据以及物理定律和算法来构建一个精确的虚拟模型。\n",
      "2. **实时同步**：虚拟模型与物理实体保持实时同步，反映出物理实体的当前状态。\n",
      "3. **分析与预测**：通过分析虚拟模型，可以预测物理实体的未来状态，识别潜在问题，并进行性能优化。\n",
      "4. **交互与反馈**：虚拟模型可以与物理实体进行交互，通过调整虚拟模型中的参数来影响物理实体的运行，或者从物理实体接收反馈来更新虚拟模型。\n",
      "\n",
      "数字孪生技术广泛应用于制造业、航空航天、能源、医疗保健、城市规划等领域，可以帮助企业提高效率、降低成本、增强安全性和可持续性。例如，在制造业中，数字孪生可以用来优化生产流程、预测设备故障、减少停机时间；在城市规划中，它可以用来模拟城市基础设施的运行，优化交通流量，提高能源效率等。\n"
     ]
    }
   ],
   "source": [
    "from llama_index.llms.openai_like import OpenAILike\n",
    "\n",
    "llm = OpenAILike(\n",
    "    model=\"openai/deepseek-chat\", \n",
    "    api_base=\"http://1.15.125.13:3033/v1\", \n",
    "    api_key=\"sk-1234\")\n",
    "\n",
    "response = llm.complete(\"什么是数字孪生？\")\n",
    "print(str(response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "eafceb2c-7381-4d63-9714-51152b4e3c36",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d987b7a0-d0a6-4806-93bb-b686e4c84d54",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.tools import FunctionTool\n",
    "\n",
    "def add(x: int, y: int) -> int:\n",
    "    \"\"\" 两个整数相加 \"\"\"\n",
    "    return x + y\n",
    "\n",
    "def mystery( x: int, y: int) -> int:\n",
    "    \"\"\"\" 一个神奇的函数 \"\"\"\n",
    "    return (x+y)*(x+y)\n",
    "\n",
    "def laobao(x: int, y: int) -> str:\n",
    "    \"\"\"这是一个laobao函数\"\"\"\n",
    "    return \"laobao says: \"+str(x*y)\n",
    "\n",
    "\n",
    "add_tool = FunctionTool.from_defaults(fn=add)\n",
    "mystery_tool = FunctionTool.from_defaults(fn=mystery)\n",
    "laobao_tool = FunctionTool.from_defaults(fn=laobao)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5cc360e3-5e38-4f36-84fd-45a7f666082c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;3;38;5;200mThought: The current language of the user is: Chinese. I need to use the mystery tool to help me answer the question.\n",
      "Action: mystery\n",
      "Action Input: {'x': 9, 'y': 8}\n",
      "\u001b[0m\u001b[1;3;34mObservation: 289\n",
      "\u001b[0m289\n"
     ]
    }
   ],
   "source": [
    "response = llm.predict_and_call(\n",
    "    [add_tool, mystery_tool, laobao_tool],\n",
    "    # \"将9和8laobao一下\",\n",
    "    # \"将9和8处理一下，需要输出为文本\",\n",
    "    \"9和8神奇的结果是什么呢？\",\n",
    "    verbose=True\n",
    ")\n",
    "\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d342a82a-7403-4857-837e-c6c35af04d7e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
